package com.gilight.base.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.gilight.base.base.ResultEntity;
import com.gilight.base.dto.SysDictDto;
import com.gilight.base.model.SysDict;
import com.gilight.base.model.SysUser;
import com.gilight.base.service.ISysDictService;
import com.gilight.base.util.ShiroUtil;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.Date;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/web/sysDict")
public class SysDictController {

    @Autowired
    private ISysDictService sysDictService;

    @RequestMapping(value = "/getDictByBzdm", method = RequestMethod.GET)
    public ResultEntity getDictByBzdm(@RequestParam String bzdm) {
        ResultEntity resultEntity = new ResultEntity();
        List<Map<String, Object>> list = sysDictService.selectByBzdm(bzdm);
        resultEntity.setData(list);
        return resultEntity;
    }

    @ApiOperation(value = "根据条件获取字典列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNo", value = "页码", required = true, paramType = "query", dataType = "int"),
            @ApiImplicitParam(name = "pageSize", value = "每页条数", required = true, paramType = "query", dataType = "int"),
            @ApiImplicitParam(name = "dictName", value = "字典名称", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "dictCode", value = "字典编号", paramType = "query", dataType = "String")
    })
    @RequestMapping(value = "/queryPageList", method = RequestMethod.POST)
    public ResultEntity queryPageList(@ApiIgnore @RequestBody SysDictDto sysDictDto) {
        ResultEntity resultEntity = new ResultEntity();
        PageInfo<SysDict> pageList = sysDictService.queryPageList(sysDictDto);
        resultEntity.setData(pageList);
        return resultEntity;
    }

    @ApiOperation(value = "添加字典")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "dictName", value = "字典名称", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "dictCode", value = "字典编号", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "description", value = "字典描述", paramType = "query", dataType = "String")
    })
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public ResultEntity add(@ApiIgnore @RequestBody SysDict sysDict) {
        ResultEntity resultEntity = new ResultEntity();
        SysUser sysUser = ShiroUtil.getUser();
        String userCode = sysUser.getUserCode();
        sysDict.setCreateBy(userCode);
        sysDict.setCreateTime(new Date());
        sysDict.setUpdateBy(userCode);
        sysDict.setUpdateTime(new Date());
        sysDict.setDelFlag(0);
        sysDictService.save(sysDict);
        return resultEntity;
    }

    @ApiOperation(value = "编辑字典")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "字典ID", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "dictName", value = "字典名称", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "dictCode", value = "字典编号", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "description", value = "字典描述", paramType = "query", dataType = "String")
    })
    @RequestMapping(value = "/edit", method = RequestMethod.POST)
    public ResultEntity edit(@ApiIgnore @RequestBody SysDict sysDict) {
        ResultEntity resultEntity = new ResultEntity();
        SysUser sysUser = ShiroUtil.getUser();
        String userCode = sysUser.getUserCode();
        sysDict.setUpdateBy(userCode);
        sysDict.setUpdateTime(new Date());
        sysDictService.updateById(sysDict);
        return resultEntity;
    }

    @ApiOperation(value = "根据ID删除字典")
    @ApiImplicitParam(name = "id", value = "字典ID", required = true, paramType = "query", dataType = "String")
    @RequestMapping(value = "/delete", method = RequestMethod.GET)
    public ResultEntity delete(@RequestParam String id) {
        ResultEntity resultEntity = new ResultEntity();
        SysUser sysUser = ShiroUtil.getUser();
        String userCode = sysUser.getUserCode();
        SysDict sysDict = new SysDict();
        sysDict.setId(id);
        sysDict.setDelFlag(1);
        sysDict.setUpdateBy(userCode);
        sysDict.setUpdateTime(new Date());
        sysDictService.updateById(sysDict);
        return resultEntity;
    }

    @ApiOperation(value = "根据字典编号查询字典对象")
    @ApiImplicitParam(name = "dictCode", value = "字典编号", required = true, paramType = "query", dataType = "String")
    @RequestMapping(value = "/getSysDictByDictCode", method = RequestMethod.GET)
    public ResultEntity getSysDictByDictCode(@RequestParam String dictCode) {
        ResultEntity resultEntity = new ResultEntity();
        SysDict sysDict = new SysDict();
        sysDict.setDictCode(dictCode);
        sysDict.setDelFlag(0);
        QueryWrapper<SysDict> query = new QueryWrapper<>(sysDict);
        List<SysDict> sysDicts = sysDictService.list(query);
        resultEntity.setData(sysDicts);
        return resultEntity;
    }
}
